using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colman.Rpg.Connection;
using Colman.Rpg.Messaging;

namespace Colman.Rpg
{
    /// <summary>
    /// Ye olde RPG shoppe.
    /// </summary>
    public partial class MarketForm : Colman.Rpg.ConnectionAwareForm
    {
        /// <summary>
        /// Creates a new instance of <see cref="MarketForm" />.
        /// </summary>
        public MarketForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Raises the <see cref="Form.Load" /> event.
        /// </summary>
        /// <param name="e">An <see cref="EventArgs" /> that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ClientEndPoint.Instance.SendMessage(new EnterShopRequest());
        }

        /// <summary>
        /// Called when a message is received from the server.
        /// </summary>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="e">The message arguments.</param>
        protected override void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            base.OnMessageReceived(sender, e);

            ItemTransactionResponse transactionResponse = e.Message as ItemTransactionResponse;

            if (transactionResponse != null)
            {
                this.RefreshPlayer(transactionResponse.Player);
                this.UpdateButtons();
            }
            else if (e.Message is EnterShopResponse)
            {
                EnterShopResponse enterResponse = (EnterShopResponse)e.Message;
                
                this.RefreshPlayer(enterResponse.Player);
                this.shopStock.DataSource = enterResponse.Items;
            }
        }

        private void RefreshPlayer(Player player)
        {
            this.playerItems.DataSource = player.Items;
            this.lblGold.Text = player.Gold.ToString();
        }

        private void UpdateButtons()
        {
            this.btnBuy.Enabled = (this.shopStock.SelectedItem != null &&
                Convert.ToInt32(this.lblGold.Text) >= this.shopStock.SelectedItem.Cost);
            this.btnSell.Enabled = (this.playerItems.SelectedItem != null);
        }

        private void shopStock_SelectedItemChanged(object sender, EventArgs e)
        {
            this.UpdateButtons();
        }

        private void playerItems_SelectedItemChanged(object sender, EventArgs e)
        {
            this.UpdateButtons();
        }

        private void btnBuy_Click(object sender, EventArgs e)
        {
            this.btnBuy.Enabled = false;
            this.btnSell.Enabled = false;
            ClientEndPoint.Instance.SendMessage(new BuyItemRequest(this.shopStock.SelectedItem));
        }

        private void btnSell_Click(object sender, EventArgs e)
        {
            this.btnBuy.Enabled = false;
            this.btnSell.Enabled = false;
            ClientEndPoint.Instance.SendMessage(new SellItemRequest(this.playerItems.SelectedItem.GetOne()));
        }
    }
}

